// pages/myInfo/myInfo.js
import {createStoreBindings} from 'mobx-miniprogram-bindings'
import {store} from '../../store/store'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        show:false,
        text:'',
        password:'',
        cookie:'',
        userInfo:{
            followeds:0,
            follows:0,
            level:0
        },
        likeNum:0,
        playlistNum:0,
        userLikePlaylist:{},
        userPlaylist:[],
        userCollectionNum:0,
        userCollectionPlaylist:[],
    },
    // 获取cookie
    async getCookie(){
        let res = await wx.getStorage({
            key:'cookie'
        })
        this.setData({
            cookie:res.data
        })
        if(this.data.cookie)
            this.getUserInfo()          
    },
    // 显示登录界面
    showLoginBox(){
        this.setData({
            show:true
        })
    },
    // 取消登录
    cancelLogin(){
        this.setData({
            show:false
        })
    },
    // 获取用户信息
    async getUserInfo(){
        const res = await wx.p.request({
            url:`${wx.root}/user/account`,
            data:{
                cookie:this.data.cookie
            }
        })
        const res2 = await wx.p.request({
            url:`${wx.root}/user/detail`,
            data:{
                uid:res.data.profile.userId
            }
        })
        const x = {};
        x.level = res2.data.level
        x.img = res2.data.profile.avatarUrl
        x.follows = res2.data.profile.follows
        x.followeds = res2.data.profile.followeds
        x.id = res2.data.profile.userId
        x.nickname = res2.data.profile.nickname
        this.setData({
            userInfo:x
        })
        this.getUserPlaylist()
    },
    // 登录
    async login(){
        const res = await wx.p.request({
            url:`${wx.root}/login/cellphone`,
            data:{
                phone:this.data.text,
                password:this.data.password
            }
        })
        if(res.data.code == 200){
            wx.setStorage({
                key:'cookie',
                data:res.data.cookie,
            })
            wx.setStorage({
                key:'uid',
                data:res.data.account.id
            })
            Toast.success({
                type:'success',
                message:'登录成功',
                onClose: async () => {
                    const c = await wx.getStorage({
                        key:'cookie',
                    })
                    this.setData({
                        cookie:c.data,
                        show:false
                    })
                    this.getUserInfo()
                },
            })
        }else{
            Toast.fail('密码错误')
        }
    },
    // 退出登录
    quit(){
        const _this = this;
       wx.removeStorage({
         key: 'cookie',
         complete(){
            Toast.success({
                type:'success',
                message:'退出成功',
                onClose:()=>{
                    _this.setData({
                        cookie:null,
                        likeNum:0,
                        playlistNum:0,
                        userCollectionNum:0,
                        userCollectionPlaylist:[],
                        userLikePlaylist:{},
                        userPlaylist:[],
                    })
                }
            })
         }
       }) 
       wx.removeStorage({
         key: 'uid',
       })
    },
    // 获取用户歌单
    async getUserPlaylist(){
        const res = await wx.p.request({
            url:`${wx.root}/user/playlist`,
            data:{
                uid:this.data.userInfo.id,
                cookie:this.data.cookie
            }
        })
        const res2 = {}
        res2.img = res.data.playlist[0].coverImgUrl
        res2.id = res.data.playlist[0].id
        const like = [];
        const collect = [];
        for (let index = 1; index < res.data.playlist.length; index++) {
            const item = res.data.playlist[index]
            if(item.userId == this.data.userInfo.id)
                like.push(item)
            else collect.push(item)
        }
        this.setData({
            likeNum:res.data.playlist[0].trackCount,
            userLikePlaylist:res2,
            userPlaylist:like,
            playlistNum:like.length,
            userCollectionNum:collect.length,
            userCollectionPlaylist:collect
        })
    },

    noop(){
        return ;
    },
    onLoad: function (options) {
        wx.showLoading({
          title: '载入中...',
        })
       this.getCookie()
       wx.hideLoading()
    },
    onUnload: function () {
        this.stroeBindings.destoryStoreBindings()
    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
})